//
//  HomeTableViewCell.swift
//  ifanr
//
//  Created by 😱😱 on 16/9/14.
//  Copyright © 2016年 😱😱. All rights reserved.
//

import UIKit
import SnapKit

enum HomeCellType {
    case Default
    case Dasheng
    case Data
    
    func cellReuseIdentifier() -> String {
        switch self {
        case .Default:
            return "HomeCellDefault"
        case .Dasheng:
            return "HomeCellDasheng"
        case .Data:
            return "HomeCellData"
        }
    }
}

class HomeTableViewCell: UITableViewCell, Reuseable {
    
    class func cellWithTableView(tableView: UITableView) -> HomeTableViewCell {
        var cell = tableView.dequeueReusableCell() as HomeTableViewCell?
        if cell == nil {
            cell = HomeTableViewCell(style: .Default, reuseIdentifier: self.reuseIdentifier)
            
        }
        
        return cell!
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clearColor()
        selectionStyle = .None
        
        switch reuseIdentifier! {
        case HomeCellType.Dasheng.cellReuseIdentifier():
            self.contentView.addSubview(d_authorImageView)
            self.contentView.addSubview(d_authorLabel)
            self.contentView.addSubview(d_likeLabel)
            self.contentView.addSubview(d_likeImageView)
            self.contentView.addSubview(d_categoryAndTimeLabel)
            self.contentView.addSubview(d_introduceLabel)
            self.contentView.layer.addSublayer(d_cellBottomLine)
            
        case HomeCellType.Data.cellReuseIdentifier():
            self.contentView.addSubview(s_label)
            
            s_label.snp_makeConstraints(closure: { (make) in
                make.center.equalTo(self.center)
            })
            
        default:
            self.contentView.addSubview(contentImageView)
            self.contentView.addSubview(categoryAndTimeLabel)
            self.contentView.addSubview(likeLabel)
            self.contentView.addSubview(likeImageView)
            self.contentView.addSubview(titleLabel)
            self.contentView.addSubview(introduceLabel)
            self.contentView.addSubview(cellBottomLine)
            
            contentImageView.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(self.contentView.snp_top).offset(UIConstant.UI_MARGIN_20)
                make.centerX.equalTo(self.contentView.snp_centerX)
                make.width.equalTo(UIConstant.SCREEN_WIDTH - 2 * UIConstant.UI_MARGIN_10)
                make.height.equalTo((UIConstant.SCREEN_WIDTH - 2 * UIConstant.UI_MARGIN_10) * 0.625)
            })
            
            categoryAndTimeLabel.snp_makeConstraints(closure: { (make) in
                make.left.equalTo(self.contentView.snp_left).offset(UIConstant.UI_MARGIN_10)
                make.top.equalTo(contentImageView.snp_bottom).offset(UIConstant.UI_MARGIN_10)
            })
            
            likeLabel.snp_makeConstraints(closure: { (make) in
                make.right.equalTo(self.contentView.snp_right).offset(-UIConstant.UI_MARGIN_10)
                make.top.equalTo(contentImageView.snp_bottom).offset(UIConstant.UI_MARGIN_10)
            })
            
            likeImageView.snp_makeConstraints(closure: { (make) in
                make.right.equalTo(likeLabel.snp_left).offset(-UIConstant.UI_MARGIN_10)
                make.centerY.equalTo(likeLabel.snp_centerY)
                make.width.equalTo(12.5)
                make.height.equalTo(11)
            })
            
            titleLabel.snp_makeConstraints(closure: { (make) in
                make.left.equalTo(categoryAndTimeLabel.snp_left)
                make.right.equalTo(likeLabel.snp_right)
                make.top.equalTo(categoryAndTimeLabel.snp_bottom).offset(UIConstant.UI_MARGIN_10)
            })
            
            introduceLabel.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(titleLabel.snp_bottom).offset(UIConstant.UI_MARGIN_10)
                make.left.equalTo(categoryAndTimeLabel.snp_left)
                make.right.equalTo(likeLabel.snp_right)
            })
            
            cellBottomLine.snp_makeConstraints(closure: { (make) in
                make.centerX.equalTo(self.contentView.snp_centerX);
                make.height.equalTo(2)
                make.width.equalTo(20)
                make.top.equalTo(introduceLabel.snp_bottom).offset(30)
            })
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: --------------------------- Getter and Setter --------------------------
    
    //MARK:---- 普通 ----
    /// 内容图片
    private lazy var contentImageView:UIImageView = {
        var contentImageView = UIImageView()
        contentImageView.image = UIImage(named:"place_holder_image")
        contentImageView.contentMode = .ScaleAspectFit
        return contentImageView
    }()
    
    ///
    private lazy var categoryAndTimeLabel:UILabel = {
        var categoryAndTimeLabel = UILabel()
        categoryAndTimeLabel.text = "商业 | 2小时前"
        categoryAndTimeLabel.textColor = UIConstant.UI_COLOR_GrayTheme
        categoryAndTimeLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 12)
        return categoryAndTimeLabel
    }()
    
    /// 喜欢数
    private lazy var likeLabel: UILabel = {
        var likeLabel = UILabel()
        likeLabel.textColor = UIConstant.UI_COLOR_GrayTheme
        likeLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 12)
        likeLabel.text = "1"
        return likeLabel
    }()
    
    private lazy var likeImageView: UIImageView = {
        var likeImage = UIImageView(image: UIImage(named: "heart_selected_false"))
        likeImage.contentMode = .ScaleAspectFit
        return likeImage
    }()
    
    /// 标题
    private lazy var titleLabel: UILabel = {
        var titleLabel = UILabel()
        titleLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 16)
        titleLabel.numberOfLines = 0
        titleLabel.textColor = UIColor.blackColor()
        titleLabel.text = "1111111111111111111111111111111"
        return titleLabel
    }()
    
    /// 引文
    private lazy var introduceLabel: UILabel = {
        var introduceLabel = UILabel()
        introduceLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 12)
        introduceLabel.numberOfLines = 0
        introduceLabel.textColor = UIConstant.UI_COLOR_GrayTheme
        introduceLabel.text = "222222222222222222222222222222"
        return introduceLabel
    }()
    
    /// 底部分割线
    private lazy var cellBottomLine: UIView = {
        var cellBottomLine = UIView()
        cellBottomLine.backgroundColor = UIConstant.UI_COLOR_GrayTheme
        return cellBottomLine
    }()

    //MARK:----- 大声 ------
    /// 作者图片
    private lazy var d_authorImageView: UIImageView = {
        var d_authorImageView = UIImageView()
        d_authorImageView.image = UIImage(named: "ic_dasheng")
        d_authorImageView.contentMode = .ScaleAspectFit
        return d_authorImageView
    }()
    
    /// 分类 和 时间
    private lazy var d_categoryAndTimeLabel: UILabel = {
        var d_categoryAndTimeLabel = UILabel()
        d_categoryAndTimeLabel.textColor = UIConstant.UI_COLOR_GrayTheme
        d_categoryAndTimeLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 12)
        return d_categoryAndTimeLabel
    }()
    
    /// 喜欢数
    private lazy var d_likeLabel: UILabel = {
        var d_likeLabel = UILabel()
        d_likeLabel.textColor = UIConstant.UI_COLOR_GrayTheme
        d_likeLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 12)
        return d_likeLabel
    }()
    
    private lazy var d_likeImageView: UIImageView = {
        var d_likeImageView = UIImageView(image: UIImage(named: "heart_selected_false"))
        d_likeImageView.contentMode = .ScaleAspectFit
        return d_likeImageView
    }()
    
    /// 引文
    private lazy var d_introduceLabel: UILabel = {
        var d_introduceLabel = UILabel()
        d_introduceLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 16)
        d_introduceLabel.numberOfLines = 0
        d_introduceLabel.textColor = UIConstant.UI_COLOR_RedTheme
        return d_introduceLabel
    }()
    
    /// 作者
    private lazy var d_authorLabel: UILabel = {
        var d_authorLabel = UILabel()
        d_authorLabel.textAlignment = .Center
        d_authorLabel.font = UIFont.customFont_FZLTXIHJW(fontSize: 16)
        d_authorLabel.textColor = UIConstant.UI_COLOR_GrayTheme
        return d_authorLabel
    }()
    
    /// 底部分割线
    private lazy var d_cellBottomLine: CALayer = {
        var d_cellBottomLine = CALayer()
        d_cellBottomLine.backgroundColor = UIConstant.UI_COLOR_GrayTheme.CGColor
        return d_cellBottomLine
    }()

    //MARK:----- 数读 ------
    //数独暂时不做啊，防止崩溃，返回固定
    
    private lazy var s_label: UILabel = {
        var s_label = UILabel()
        s_label.text = "这是一个数读"
        s_label.font = UIFont.customFont_FZLTZCHJW(fontSize: 16)
        return s_label
    }()
}
